

package biofilter.pipelines;

import biofilter.exceptions.InvalidRecordException;
import biofilter.sources.RecordsFile;
import biofilter.filters.BowtieFilter;
import biofilter.filters.CompositeFilter;
import biofilter.records.Record;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daniel Quest
 * @date   Dec 31 2009
 */
public class BowtieMapperPipeline extends PipelineBase {

    private RecordsFile fnrf = null;
    private BowtieFilter bowfil = null;
    String unalignedReadfile = "";
    String alignedReadfile = "";
    public BowtieMapperPipeline(String fileNameRecordsFile) throws Exception {
        super();
        //        this.logger = Logger.getLogger(this.getClass().getName());
        this.filters = new CompositeFilter();
        fnrf = new RecordsFile(fileNameRecordsFile,"biofilter.records.FileNameRecord");
        this.filters.setSource(fnrf);
        bowfil = new BowtieFilter(getDefaultBowTieOptions());
        this.filters.addFilter( bowfil );
    }

    private String getDefaultBowTieOptions(){
        return " -f -v 3 -a -p 4 --best --strata --strandfix --quiet ";
    }

    private String getUserBowTieOptions(){
        String opts = getDefaultBowTieOptions() + this.alignedReadfile + this.alignedReadfile;
        return opts;
    }

    public void setUnalignedReadFile(String readFile){
        this.unalignedReadfile = " --un " + readFile + " ";
    }

    public void setAlignedReadFile(String readFile){
        this.alignedReadfile = " --al " + readFile + " ";
    }

    public void setDatabaseFile(String filename) throws FileNotFoundException {
         File file=new File(filename + ".1.ebwt");
         if(!file.exists()) throw new FileNotFoundException("NO ebwt bowtie database!");
            this.bowfil.setEbwt(filename);
    }

    public void setReadFile(String fileNameRecordsFile)throws FileNotFoundException {
        try {
            File file = new File(fileNameRecordsFile + ".1.ebwt");
            if (!file.exists()) {
                throw new FileNotFoundException("problem getting read file source!");
            }
            this.bowfil.setHitFileName(fileNameRecordsFile);
            fnrf = new RecordsFile(fileNameRecordsFile, "biofilter.records.FileNameRecord");
            this.filters.setSource(fnrf);
        } catch (InvalidRecordException ex) {
            Logger.getLogger(BowtieMapperPipeline.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Vector<Record> execPipeline() throws Exception {
        Vector<Record> resultFiles = new Vector<Record>();
        if (!this.alignedReadfile.equalsIgnoreCase("")) bowfil.setOptions(this.getUserBowTieOptions());
        if (!this.unalignedReadfile.equalsIgnoreCase("")) bowfil.setOptions(this.getUserBowTieOptions());
        while (this.filters.eof() != true) {
            resultFiles.add(this.filters.getRecord());
            System.out.println(resultFiles.firstElement().get());
        }
        return resultFiles;
    }

    public static void main(String[] args) {
        try {
            BowtieMapperPipeline btmp = new BowtieMapperPipeline(args[0]);
            //btmp.setDatabaseFile("/auto/transcriptomics/data/genomes/Bacteria/Burkholderia_cenocepacia_HI2424/NC_008542");
            //btmp.setReadFile("/auto/transcriptomics/data/transcripts/Bacteria/Burkholderia_cenocepacia_HI2424/cDNA_CF_media_rep1.fa");
            btmp.execPipeline();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

}
